package webs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.pojo.Replys;
import dao.pojo.Topics;
import dao.pojo.Users;
import domain.ApplicationContext;
import domain.BoardFacade;

public class GetReply extends HttpServlet{

	private BoardFacade boardService;
	
	public BoardFacade getBoardService() {
		return boardService;
	}

	public void setBoardService(BoardFacade boardService) {
		this.boardService = boardService;
	}
	
    @Override
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doPost(httpServletRequest,httpServletResponse);
    }

    @Override
    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        
    	httpServletRequest.setCharacterEncoding("UTF-8");
    	BoardFacade boardService=(BoardFacade)ApplicationContext.get("BoardService");
    	
    	HttpSession session = httpServletRequest.getSession();
    	Users user = (Users) session.getAttribute("login_user");
    	if(user==null) httpServletResponse.sendRedirect("login.jsp");
    	else 
    	{
    		String content = httpServletRequest.getParameter("Content");
    		Topics topic = (Topics) session.getAttribute("topic");
    		Date submitTime = new Date();
    		Replys reply = new Replys();
    		reply.setContent(content);
    		reply.setTopic(topic);
    		reply.setUser(user);  
    		reply.setId(topic.getId());
    		reply.setSubmitTime(submitTime);
    		boardService.addReply(reply);
    		ArrayList<Replys> replyList = boardService.getAllReplys(topic);
    		session.setAttribute("replyList", replyList);
    		httpServletResponse.sendRedirect("reply.jsp");
    	}
    	//if(boardService.addReply(reply)) httpServletResponse.sendRedirect("GetTopic");
    	
    }

    @Override
    public void destroy() {
        super.destroy();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void init() throws ServletException {
        super.init();    //To change body of overridden methods use File | Settings | File Templates.
    }
}
